import api from './api';

export interface LoginParams {
  username: string;
  password: string;
  captcha: string;
}

export interface RegisterParams {
  username: string;
  password: string;
  captcha: string;
  phone?: string;
}

export const getCaptcha = () => {
  return api.get('/captcha/create');
};

export const login = (params: LoginParams) => {
  // 使用FormData格式发送请求
  const formData = new FormData();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null) {
      formData.append(key, value.toString().trim());
    }
  });
  
  // 打印请求参数（不包含密码）
  console.log('Login params:', {
    username: params.username,
    captcha: params.captcha
  });
  
  return api.post('/user/login', formData);
};

export const register = (params: RegisterParams) => {
  // 使用FormData格式发送请求
  const formData = new FormData();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null) {
      formData.append(key, value.toString().trim());
    }
  });
  
  return api.post('/user/register', formData);
}; 